//
//  StoreListTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/25.
//

import UIKit

class StoreListTableViewCell: UITableViewCell {
    var collectionView: UICollectionView!
    var dataArray: [VehicleLisetModel] = []
    var promotionId = 0//活动ID
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.clipsToBounds = true
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        layout.scrollDirection = .vertical
        let ww = (kScreenWidth - 38)/2
        layout.itemSize = CGSize(width: ww, height: 210)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
        self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 10, width: kScreenWidth, height: 170), collectionViewLayout: layout)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.register(GoodsCollectionViewCell.self, forCellWithReuseIdentifier: "GoodsCollectionViewCell")
        self.collectionView.backgroundColor = .white
        self.contentView.addSubview(self.collectionView)
        self.collectionView.showsVerticalScrollIndicator = false
        self.collectionView.isScrollEnabled = false
    
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(500)
            make.bottom.equalTo(-10).priority(.high)
        }
    }
    
    
    //setModel
    func setData(data: [VehicleLisetModel]){
        self.dataArray = data
        self.collectionView.reloadData()
        self.collectionView.layoutSubviews()
        let hh = self.collectionView.collectionViewLayout.collectionViewContentSize.height
        self.collectionView.snp.updateConstraints { make in
            make.height.equalTo(hh)
        }
        self.updateConstraints()
    }
    

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}



extension StoreListTableViewCell: UICollectionViewDataSource,UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GoodsCollectionViewCell", for: indexPath) as! GoodsCollectionViewCell
        cell.collectBtn.isHidden = false
        if self.dataArray.count > indexPath.row{
            let model = self.dataArray[indexPath.row]
            if self.promotionId == -1{
                cell.setStoreModel(storeModel: model)
            }else{//活动ID
                cell.setFlashSaleModel(model: model)
            }
           
        }
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.dataArray.count > indexPath.row{
            let model = self.dataArray[indexPath.row]
            let vc = ProductDetailsViewController()
            vc.id = model.id ?? 0
            self.viewController().navigationController?.pushViewController(vc, animated: true)
        }
    }
    
   
}
